package com.wh.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import com.wh.beans.ConstVar;
import com.legend.utils.EhCacheUtil;

public class MySqlSessionFactory {
	static Logger log = Logger.getRootLogger();
	final static String CACHENAME = ConstVar.FEREVER_CACHE;
	final static String CACHEKEY = "SqlSessionFactory";
	private SqlSessionFactory sqlSessionFactory;

	public SqlSessionFactory getSessionFactory() {
		Object object = EhCacheUtil.getInstance().get(CACHENAME, CACHEKEY);
		if (object != null) {
			log.info("从缓存区取值");
			sqlSessionFactory = (SqlSessionFactory) object;
		} else {
			log.info("没有缓存~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!");
		}
		return sqlSessionFactory;
	}

	public void setSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
		log.info("对象的值:" + sqlSessionFactory);
		EhCacheUtil.getInstance().put(CACHENAME, CACHEKEY, sqlSessionFactory);
	}

	public SqlSession getSqlSession() {
		return getSessionFactory().openSession();
	}
}
